<?php
$BreadCrumb = FALSE;
$welcome = "BC-next";
$prescriptions = "BC-current";
$nonprescriptions = "BC-next";

function MyErrorMessage($InstructionTitle, $InstructionDetails, $LargeButton1Text, $LargeButton2Text, $ContinuePage, $NextPage) {

    include 'includes/wizard-simple-header.php';
?>
<div class="invisible-border">
<?php
    include 'includes/wizard-large-button.php';
    include './includes/wizard-simple-footer.php';
?>
</div>
<?php
}

if (isset($_POST['Number']))
  {
    $Number = $_POST['Number'];
    if (strlen($Number) == 0)
      {
	$InstructionTitle = "ERROR: No Medication Entered";
	  $InstructionDetails = "A medication was not entered. Please go back to reenter a medication number or go back to your medication list.";
	$LargeButton1Text = "Enter Medication &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;";
	$LargeButton2Text = "View Medication List&#160;&#160;";

	$NonPrescription = $_GET['NonPrescription'];
	
	if ($NonPrescription){
	  $ContinuePage = "./barcode.php?NextPage=add-medication.php&Wizard=1&NonPrescription=1";
	  $NextPage = "./nonprescriptions.php";
	} else {
	  $ContinuePage = "./barcode.php?NextPage=add-medication.php&Wizard=1&NonPrescription=0";
	  $NextPage = "./missing-prescriptions.php";
	}
	
	MyErrorMessage($InstructionTitle, $InstructionDetails, $LargeButton1Text, $LargeButton2Text, $ContinuePage, $NextPage);

      } else
      {
	// we have a legit number...
	$InstructionTitle = "Prescription Verification";
	$InstructionDetails ="The medication you entered is listed below. Please click \"Add Medication\" to add the medication or \"Cancel\" to not add the medication.";
	$LargeButton1Text = "Add Medication &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;";
	$LargeButton2Text = "Cancel&#160;&#160;";

	$NonPrescription = $_GET['NonPrescription'];
	if ($NonPrescription){
	  $ContinuePage = "./nonprescriptions.php";
	  $NextPage = "./nonprescriptions.php";
	} else {
	  $ContinuePage = "./missing-prescriptions.php";
	  $NextPage = "./missing-prescriptions.php";
	}
    include 'includes/wizard-simple-header.php';
?>
<div class="invisible-border">
<div class="left-box">
<?php
   include 'includes/db-login.php';
  
    $query = 'SELECT * FROM SearchNDC WHERE NDC = \''.$Number.'\' OR barcode = \''.$Number.'\'';
    //    echo $query;

    $result = mysql_query($query) or die ('Query failed: '. mysql_error());
    $numresults = mysql_num_rows($result);

    if ($numresults == 0){
      // number (NDC or barcode) was not found in database
      $InstructionTitle = "ERROR: Medication NDC Code or Barcode was not found.";
	$InstructionDetails = "The medication NDC Code or Barcode was not found. Please go back to reenter a medication number or go back to your medication list.";
	$LargeButton1Text = "Enter Medication &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;";
	$LargeButton2Text = "View Medication List&#160;&#160;";


	if ($NonPrescription){
	  $ContinuePage = "./barcode.php?NextPage=add-medication.php&Wizard=1&NonPrescription=1";
	  $NextPage = "./nonprescriptions.php";
	} else {
	  $ContinuePage = "./barcode.php?NextPage=add-medication.php&Wizard=1&NonPrescription=0";
	  $NextPage = "./missing-prescriptions.php";
	}
	
	MyErrorMessage($InstructionTitle, $InstructionDetails, $LargeButton1Text, $LargeButton2Text, $ContinuePage, $NextPage);
      
    } else {
      //number (NDC or barcode) found in database
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	
	echo "You have entered ".$Number.":<BR> {$row['MedicationIdentity']} ({$row['UnitStrength']})<BR><i> ({$row['GenericIngredients']}) </i>";

	$NonPrescription = $_GET['NonPrescription'];
	
	if ($NonPrescription){
	  $ContinuePage = $ContinuePage."?AddMed={$row['MedicationIdentity']}~{$row['GenericIngredients']}~{$row['UnitStrength']}&NonPrescription=1&Wizard=1";
	} else {
	  $ContinuePage = $ContinuePage."?AddMed={$row['MedicationIdentity']}~{$row['GenericIngredients']}~{$row['UnitStrength']}&NonPrescription=0&Wizard=1";
	}

      } // end of while $row
    } // else number found
   include 'includes/db-close.php';
?>
</div>
<?php
    include 'includes/wizard-large-button.php';
    include './includes/wizard-simple-footer.php';
?>
</div>
<?php

	// find it in the database
	// verify they want to add it... 
      }
  }




// Don't think I need this anymore
//$ButtonText = "Submit";
//$ContinuePage = "missing-prescriptions-add.php";
//$NextPage = "nonprescriptions.php";
//$LargeButton1Text = "Yes &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;";
//$LargeButton2Text = "Continue...&#160;&#160;";

?>
